﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core;
using System.Data;

namespace DataAccess.Classes
{
  public  class TinNhan
  {
      public int IDTinNhan { get; set; }
      public string NoiDung { get; set; }
      public string IDNguoiNhan { get; set; }
      public string NguoiGuiID { get; set; }
      public bool TinhTrang { get; set; }
      public DateTime Ngay { get; set; }


      public TinNhan() { }

      public static List<TinNhan> All()
      {
          return CBO.FillCollection<TinNhan>(DataProvider.Instance.ExecuteReader("TinhNhan_All"));
      }
      public static List<TinNhan> TinChuaDoc(string nguoidung)
      {
          return CBO.FillCollection<TinNhan>(DataProvider.Instance.ExecuteReader("TinNhan_DocThongBaoTinNhan", nguoidung));
      }


      public static List<TinNhan> TinDaDoc(string nguoidung)
      {
          return CBO.FillCollection<TinNhan>(DataProvider.Instance.ExecuteReader("TinNhan_TinNhanDaDoc", nguoidung));
      }
      public static List<TinNhan> LayDanhSachTinDaDoc(string nguoidung, string page, out int howManyPages)
      {
          IDataReader reader = null;
          try
          {
              //int pageSize = GlobalConfiguration.PageSize;
              int pageSize = 8;
              reader = DataProvider.Instance.ExecuteReader("TinNhan_DaDocPhanTrang", nguoidung,
                                                          GlobalConfiguration.DescriptionLength, Convert.ToInt32(page),
                                                          pageSize);
              reader.Read();
              howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) / (double)pageSize);
              reader.NextResult();
              return CBO.FillCollection<TinNhan>(reader);
          }
          catch
          {
              if (reader != null && reader.IsClosed == false) reader.Close();
              howManyPages = 0; return new List<TinNhan>();
          }
      }


      public static bool Them(TinNhan tn)
      {
          object rs = DataProvider.Instance.ExecuteNonQueryWithOutput("IDTinNhan", "TinNhan_Them", tn.IDTinNhan, tn.NoiDung, tn.IDNguoiNhan, tn.NguoiGuiID, tn.TinhTrang, tn.Ngay);
          return Convert.ToInt32(rs) > 0;
      }
      public static bool CapNhatTinDaDoc(string nguoinhan)
      {
          object rs = DataProvider.Instance.ExecuteNonQuery("TinNhan_CapNhatTinDaDoc", nguoinhan);
          return Convert.ToInt32(rs) > 0;
      }
      public static int TinMoi(string nguoidung)
      {
          try
          {
              object rs = DataProvider.Instance.ExecuteScalar("TinNhan_ThongBaoTinNhan", nguoidung);
              return Convert.ToInt32(rs);
          }
          catch
          {
              return Convert.ToInt32(0);
          }
      }
  }
}
